<template>
  <div class="login">
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)"/>
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input v-model="phoneNumber" class="inp" maxlength="11" placeholder="请输入手机号码" type="text">
        </div>
        <div class="form-item">
          <input v-model="picCode" class="inp" maxlength="5" placeholder="请输入图形验证码" type="text">
          <img v-if="picUrl" :src="picUrl" @click="getPicCode" alt="">
        </div>
        <div class="form-item">
          <input v-model="smsCode" class="inp" placeholder="请输入短信验证码" type="text">
          <button @click="getCode">
            {{ second === totalSecond ? '获取验证码' : second + '秒后重新获取' }}
          </button>
        </div>
      </div>

      <van-button @click="login" round type="info">登录</van-button>
    </div>
  </div>
</template>

<script>
import { getPicCode, getSmsCode, smsCodeLogin } from '@/api/login'
import { Toast } from 'vant'
// import { Toast } from 'vant'
export default {
  name: 'LoginPage',
  data () {
    return {
      picKey: '', // 请求回来的图形验证码的唯一标识
      picUrl: '', // 存储请求渲染的图片地址
      totalSecond: 60, // 总秒数
      second: 60, // 当前秒数
      timer: null, // 定时器id
      phoneNumber: '', // 电话号码
      picCode: '', // 用户输入的图形验证码
      smsCode: '' // 短信验证码
    }
  },
  methods: {
    // 获取图形验证码
    async getPicCode () {
      const { data: { base64, key } } = await getPicCode()
      this.picUrl = base64
      this.picKey = key
    },
    // 获取短信验证码
    async getCode () {
      // 未通过校验直接结束
      if (!this.verify()) return
      // 定时器不存在且second归位时获取
      if (!this.timer && (this.second === this.totalSecond)) {
        // 发送短信验证码请求
        await getSmsCode(this.picCode, this.picKey, this.phoneNumber)
        // 开启定时器
        this.timer = setInterval(() => {
          this.second--
          // second = 0 时归位
          if (this.second <= 0) {
            clearInterval(this.timer)
            this.timer = null
            this.second = this.totalSecond
          }
        }, 1000)
      }
    },
    // 校验手机号和图形验证码是否正确
    verify () {
      if (!/^1[3-9]\d{9}$/.test(this.phoneNumber)) {
        Toast('请输入正确的手机号！')
        return false
      }
      if (!/^[0-9a-zA-Z]{4}$/.test(this.picCode)) {
        Toast('图形验证码输入错误！')
        return false
      }
      return true
    },
    // 登录
    async login () {
      if (!this.verify()) return
      if (!/^\d{6}$/.test(this.smsCode)) {
        this.$toast('请输入正确的短信验证码！')
        return
      }
      const result = await smsCodeLogin(this.phoneNumber, this.smsCode)
      this.$store.commit('user/setUserInfo', result.data)
      this.$toast('登录成功！')

      /* 判断是否有跳回地址
      *  如果有 说明是其他页面拦截到登录页 需要回跳
      *  如果没有 正常去首页
      */
      const url = this.$route.query.backUrl || '/'
      await this.$router.replace(url)
    }
  },
  // 进入页面发送请求获取图形验证码
  async created () {
    await this.getPicCode()
    // // 全局可用
    // Toast('图形验证码获取成功！')
    // // 组件范围内，无需导入
    // this.$toast.success('验证码获取成功！')
  },
  // 离开页面时清除定时器
  destroyed () {
    clearInterval(this.timer)
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;

    h3 {
      font-size: 26px;
      font-weight: normal;
    }

    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;

    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;

      &:focus {
        caret-color: #1989fa;
      }
    }

    img {
      width: 94px;
      height: 31px;
    }

    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #1989fa;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .van-button--round {
    width: 100%;
    height: 42px;
    margin-top: 39px;
  }
}
</style>
